package com.fruit.service.lightpay.impl;


import java.io.PrintWriter;
import java.io.StringWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.fruit.mapper.lightpay.LightPayMapper;
import com.fruit.pojo.lightpay.LightPayException;
import com.fruit.util.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class LightPayLogService{


	
	@Autowired
	private LightPayMapper lightPayMapper;	
	
	public void newSaveExceptionLightPay(String tradeId,String serviceName,String serviceState,String cashierCode,int userId, String code, String message){
		try{
		log.info("[{}]-【收银异常日志】-【收银流水号：{}】",tradeId, cashierCode);
		LightPayException lpe = new LightPayException();
		lpe.setTradeId(tradeId);
		lpe.setServiceName(serviceName);
		lpe.setCashierCode(cashierCode);
		lpe.setUserId(userId);
		JSONObject paJson=new JSONObject();
		paJson.put("code", cashierCode);
		lpe.setInputParams(paJson.toString());
		lpe.setServiceState(serviceState);
		lpe.setErrorCode(code);
		lpe.setErrorMessage(message);
		lpe.setQueryTime(DateUtil.getTime());
		lightPayMapper.saveExceptionLightPay(lpe);
		log.info("[{}]-【收银异常日志】-【成功】",tradeId);
		}catch(Exception e){
			log.error("[{}]-【收银异常日志】-【异常】------【{}】",tradeId,getStackTraceAsString(e));
		}
		log.info("[{}]-【收银异常日志】-【结束】",tradeId);
		
	}

	public String getStackTraceAsString(Throwable ex) {
		StringWriter stringWriter = new StringWriter();
		ex.printStackTrace(new PrintWriter(stringWriter));
		return stringWriter.toString();
	}
	
}
